package com.ebanca.drawme;

import java.util.ArrayList;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.ebanca.drawme.R;

/**
 * Custom list view used to provide a common way of handling 
 * the BinC application views of map, event log and follow-up 
 * screens. 
 */
public class CustomListView extends ListView implements ViewMenuDirectionNavigation {
	
	protected ArrayList dataProvider;
	protected CustomListAdapter listAdapter;


	public CustomListView(Context context, AttributeSet attributeSet) {
		super(context, attributeSet);
		init();
	}
	
	protected void init(){
		this.setChoiceMode(CHOICE_MODE_SINGLE);
		this.setFocusable(true);
		this.setDividerHeight(0);
		this.setBackgroundColor(getResources().getColor(R.color.backgroundColor));
		setData();
	}
	
	protected void setData() {
		setAdaptor();
	}
	
	protected void setAdaptor(){
		setAdapter(listAdapter);
		setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView adapterView, View view, int position, long id) {
				// user clicked a list item, make it "selected"
				listAdapter.setSelectedPosition(position);
			}
        });
		if(listAdapter.selectedPos==-1)
			listAdapter.setSelectedPosition(0);
		else 
			listAdapter.setSelectedPosition(listAdapter.selectedPos);
	}
	
	/**
	 * Implementation of the methods from ViewMenuDirectionNavigation interface. 
	 */
	
	@Override
	public void onDown() {
		int selectedPos = listAdapter.getSelectedPosition();
    	if (selectedPos < dataProvider.size()-1 ){
    		// set selected position in the adapter
    		listAdapter.setSelectedPosition(selectedPos+1);
    	}
	}

	@Override
	public void onLeft() {
		// Do nothing.
	}

	@Override
	public void onRight() {
		// Do nothing.
	}

	@Override
	public void onUp() {
		int selectedPos = listAdapter.getSelectedPosition();
    	if (selectedPos > 0 ){
    		// set selected position in the adapter
    		listAdapter.setSelectedPosition(selectedPos-1);
    	}
	}
	
	@Override
	public void onEnter() {
		// Do nothing.
	}	
	
	public void onRefresh() {
		if (listAdapter != null) {
			listAdapter.notifyDataSetChanged();
		}
	}
}
